<?php

namespace app\api\controller\Interact;
use app\api\controller\Common;
use app\api\Logic\CommonLogic;
use app\api\Logic\Interact\InteractLogic;
use app\api\model\InteractModel;
use think\Db;

/**
 * 互动讨论
 */
class Interact extends Common
{
    protected $noNeedLogin = ['*'];

    protected $interactLogic;
    protected $commonLogic;

    public function _initialize()
    {
        parent::_initialize();

        $this->interactLogic = new InteractLogic();
        $this->commonLogic = new CommonLogic();
    }

    /**
     * 获取互动讨论主题
     *
     */
    public function getInteractTheme()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            //验证数据
            $param = $this->verifyData($param);

            $where = [
                'a.type' => 5,
                'a.shop_id' => ['in',[0,$param['shop_id']]]
            ];
            $list = $this->interactLogic->getThemeList($where);
            $this->success('查询成功',$list);
        }
        $this->error('请求错误');
    }

    /**
     * 获取推荐互动讨论主题
     *
     */
    public function getInteractThemeRecommend()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            //验证数据
            $param = $this->verifyData($param);

            $where = [
                'a.type' => 5,
                'a.shop_id' => ['in',[0,$param['shop_id']]],
                'a.is_top' => 1
            ];
            $list = $this->interactLogic->getThemeList($where);
            $this->success('查询成功',$list);
        }
        $this->error('请求错误');
    }


    /**
     * 发布互动讨论
     *
     */
    public function releaseInteract()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            //验证数据
            $param = $this->verifyData($param);

            // 验证互动主题字段
            $verify_res = $this->interactLogic->interactVerifyData($param);
            if($verify_res){
                $this->error($verify_res);
            }
            $save_res = $this->interactLogic->saveInteract($param);
            if($save_res){
                $this->success('发布成功');
            }
            $this->error('发布失败');
        }
        $this->error('请求错误');
    }

    /**
     * 获取贴子列表
     *
     * @return void
     */
    public function getInteractList()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            //验证数据
            $param = $this->verifyData($param);
            $theme_id = input('theme_id',0);
            $order_type = input('order_type',1);

            $list = $this->interactLogic->getInteractList($param['shop_id'],$param['user_id'],$param['page'],$param['page_size'],$theme_id,$order_type);
            $this->success('获取成功',$list);
        }
        $this->error('请求错误');
    }

    /**
     * 获取贴子详情
     *
     */
    public function getInteractDetail()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            //验证数据
            $param = $this->verifyData($param);

            if(empty($param['relation_id'])){
                $this->error('未传入贴子id');
            }
            $data = $this->interactLogic->getInteractDetail($param);
            if(!$data){
                $this->error('贴子不存在或未审核');
            }
            $this->success('获取成功',$data);
        }
        $this->error('请求错误');
    }

    /**
     * 互动讨论贴子点赞
     *
     */
    public function clickLikes()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            //验证数据
            $param = $this->verifyData($param);

            if(empty($param['relation_id'])){
                $this->error('未传入贴子id');
            }

            $res = $this->commonLogic->giveLikes($param['user_id'],$param['shop_id'],$param['relation_id'],'Interact','interact','like_num');
            if($res){
                $this->success('操作成功',['status'=>$res]);
            }
            $this->error('操作失败');
        }
        $this->error('请求错误');
    }

    /**
     * 贴子分享成功
     *
     */
    public function shareSuccess()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            //验证数据
            $param = $this->verifyData($param);

            if(empty($param['relation_id'])){
                $this->error('未传入贴子id');
            }
            $res = $this->commonLogic->handleShare($param['user_id'],$param['shop_id'],$param['relation_id'],'Interact','interact','share_num');
            if($res){
                $this->success('操作成功');
            }
            $this->error('操作失败');
        }
        $this->error('请求错误');
    }

    /**
     * 评论提交
     *
     * @return void
     */
    public function releaseInteractComment()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            //验证数据
            $param = $this->verifyData($param);
            $to_id = input('to_id',0);
            if(empty($param['relation_id']) || empty($param['content']) ){
                $this->error('参数不完整');
            }
            $res = $this->commonLogic->insertComment($param['shop_id'],'Interact',2,$param['relation_id'],$param['user_id'],$param['content'],$to_id);
            if($res){
                $this->success('发布成功');
            }
            $this->error('发布失败');
        }
        $this->error('请求错误');
    }

    /**
     * 获取评论列表
     *
     */
    public function interactCommentList()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            //验证数据
            $param = $this->verifyData($param);

            if(empty($param['relation_id'])){
                $this->error('未传入贴子id');
            }
            $list = $this->commonLogic->getCommentList('Interact',$param['relation_id'],$param['page'],$param['page_size']);
            $where = ['a.style' => 'Interact','a.relation_id' => $param['relation_id'],'a.deletetime' => null];
            $count = model('CommentModel')->alias('a')->where($where)->count();
            $count = 1;

            $this->success('获取成功',['count'=>$count,'list'=>$list]);
        }
        $this->error('请求错误');
    }

    public function ttt()
    {
        dump(1);exit;
        $a = $this->interactLogic->getInteractList(1,1,1,10);
        $this->success('1',$a);


        $commonLogic = new CommonLogic();
        $shop_id = 1;
        $shop_arr = [0,$shop_id];
        $where = [
            'a.status'=>1,
            'a.shop_id'=>['in',$shop_arr]
        ];
  
        $a = new InteractModel();
        $res = $a
        ->alias('a')
        ->with(['comment','theme'])
        ->where($where)
        ->join('comment b',"a.id = b.relation_id and b.style = 'Interact'",'left')
        ->group('a.id')
        ->order('a.is_top desc,a.shop_id asc,a.weigh desc,a.createtime desc')
        ->field('a.*,count(b.id) as comment_num')
        ->select();
        if($res){
            foreach ($res as $v) {
                //获取作者头像信息
                $user_info = $commonLogic->getUserInfo($v['type'],$v['relation_id']);
                $v['nickname'] = $user_info['nickname'];
                $v['avatar'] = $user_info['avatar'];
                if($v['comment']){//存在评论 评论用户也加上昵称
                    foreach ($v['comment'] as $comment_v) {
                        $comment_user_info = $commonLogic->getUserInfo($comment_v['reply_type'],$comment_v['reply_id']);
                        $comment_v['nickname'] = $comment_user_info['nickname'];
                    }
                }   
            }
        }

        foreach ($res as $v) {
            dump($v->toArray());
        }
        exit;
        $data = $this->interactLogic->getInteractCommentList();

        
        

        exit;
    }



    
}
